package com.harvest.scientific.ladder.page.fa

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.core.view.updateLayoutParams
import com.harvest.scientific.ladder.R
import com.harvest.scientific.ladder.base.BaNoActivity
import com.harvest.scientific.ladder.base.BaseFragment
import com.harvest.scientific.ladder.constant.PageArgs
import com.harvest.scientific.ladder.databinding.LanguageSelectFragmentBinding
import com.harvest.scientific.ladder.databinding.SingleFrameLayoutBinding
import com.harvest.scientific.ladder.ext.dp2px
import com.harvest.scientific.ladder.ext.gone
import com.harvest.scientific.ladder.ext.setMaterialDrawable
import com.harvest.scientific.ladder.ext.visible
import com.harvest.scientific.ladder.page.LoginActivity
import com.harvest.scientific.ladder.util.FlowBus
import com.harvest.scientific.ladder.util.FlowBusConstant
import com.harvest.scientific.ladder.util.LocaleHelper
import com.harvest.scientific.ladder.util.LogUtils
import com.harvest.scientific.ladder.util.SpUtil
import com.harvest.scientific.ladder.util.VcUtil
import com.harvest.scientific.ladder.widget.BaseActionBar

/**
 * @author：zenglingwen
 * @date：2024-06-11 14:07
 * @desc：
 */
class LanguageSelectFragment: BaseFragment<LanguageSelectFragmentBinding>() {

    override fun initPage() {
        super.initPage()
        val isZh = LocaleHelper.getPersistedData(requireActivity(), "fr") == "zh"
        LogUtils.d("LanguageSelectFragment, initPage isZh: $isZh")

        if (!SpUtil.isLogin()) {
            _binding.exitLoginBtn.visible(false)
        }

        _binding.switchCircleViewFr.setMaterialDrawable(requireActivity().resources.getDimension(R.dimen.dp_10), Color.WHITE)
        _binding.switchCircleViewZh.setMaterialDrawable(requireActivity().resources.getDimension(R.dimen.dp_10), Color.WHITE)
        setBtnStyle(isZh)
        _binding.switchFrenchBtn.setOnClickListener {
            if (VcUtil.isFastDoubleClick(it)) {
                return@setOnClickListener
            }
            LocaleHelper.setLocale(requireActivity(), if (isZh) "fr" else "zh")
            LanguageSelectActivity.jump(requireActivity(), false)
            FlowBus.with<Unit>(FlowBusConstant.KILL_SETTING_PAGE).register(this@LanguageSelectFragment) {
                requireActivity().finish()
                FlowBus.post (FlowBusConstant.UPDATE_LOCALE, Unit)
            }
        }
        _binding.switchChineseBtn.setOnClickListener {
            if (VcUtil.isFastDoubleClick(it)) {
                return@setOnClickListener
            }

            LocaleHelper.setLocale(requireActivity(), if (isZh) "fr" else "zh")

            LanguageSelectActivity.jump(requireActivity(), false)
            FlowBus.with<Unit>(FlowBusConstant.KILL_SETTING_PAGE).register(this@LanguageSelectFragment) {
                requireActivity().finish()
                FlowBus.post (FlowBusConstant.UPDATE_LOCALE, Unit)
            }
        }

        _binding.exitLoginBtn.apply {
            setMaterialDrawable(resources.getDimension(R.dimen.dp_22), ContextCompat.getColor(requireActivity(), R.color.color_28359B))

            setOnClickListener {
                if (VcUtil.isFastDoubleClick(it)){
                    return@setOnClickListener
                }
                logout()
                LoginActivity.jump(requireActivity(), PageArgs.FROM_MINE)
                FlowBus.post(FlowBusConstant.REFRESH_HOME, Unit)
                requireActivity().finish()
            }
        }
    }

    private fun logout() {
        SpUtil.put(SpUtil.KYE_USER_ID, "")
        SpUtil.put(SpUtil.KYE_USER_TOKE, "")
        SpUtil.put(SpUtil.KYE_USER_PHONE, "")
        SpUtil.put(SpUtil.KYE_USER_TEST_CUST_FLAG, "")
        SpUtil.put(SpUtil.KYE_USER_NEW_CUST_FLAG, "")
    }

    private var isFirstResume = true

    override fun onResume() {
        super.onResume()
        if (isFirstResume) {
            isFirstResume = false
            FlowBus.post(FlowBusConstant.KILL_SETTING_PAGE, Unit)
        }
    }


    override fun setStatus(actionBar: BaseActionBar) {
        super.setStatus(actionBar)
        actionBar.apply {
            setBlackMode(true)
            setBackListener {
                requireActivity().finish()
            }
        }
    }

    private fun setBtnStyle(isZh: Boolean) {
        _binding.defaultLocaleContainer.setMaterialDrawable(dp2px(10f), ContextCompat.getColor(requireActivity(), R.color.color_f2f2f2))
        _binding.zhLocaleContainer.setMaterialDrawable(dp2px(10f), ContextCompat.getColor(requireActivity(), R.color.color_f2f2f2))
        if (isZh) {
            _binding.switchFrenchBtn.setMaterialDrawable(
                requireActivity().resources.getDimension(R.dimen.dp_22),
                ContextCompat.getColor(requireActivity(), R.color.color_c5c5c5)
            )
            _binding.switchChineseBtn.setMaterialDrawable(
                requireActivity().resources.getDimension(R.dimen.dp_22),
                ContextCompat.getColor(requireActivity(), R.color.color_28359B)
            )
            _binding.switchCircleViewZh.updateLayoutParams<RelativeLayout.LayoutParams> {
                addRule(RelativeLayout.ALIGN_PARENT_END)
                marginEnd = dp2px(2f).toInt()
            }
            _binding.switchCircleViewFr.updateLayoutParams<RelativeLayout.LayoutParams> {
                addRule(RelativeLayout.ALIGN_PARENT_START)
                marginStart = dp2px(2f).toInt()
            }
        } else {
            _binding.switchChineseBtn.setMaterialDrawable(
                requireActivity().resources.getDimension(R.dimen.dp_22),
                ContextCompat.getColor(requireActivity(), R.color.color_c5c5c5)
            )
            _binding.switchFrenchBtn.setMaterialDrawable(
                requireActivity().resources.getDimension(R.dimen.dp_22),
                ContextCompat.getColor(requireActivity(), R.color.color_28359B)
            )
            _binding.switchCircleViewZh.updateLayoutParams<RelativeLayout.LayoutParams> {
                addRule(RelativeLayout.ALIGN_PARENT_START)
                marginStart = dp2px(2f).toInt()
            }
            _binding.switchCircleViewFr.updateLayoutParams<RelativeLayout.LayoutParams> {
                addRule(RelativeLayout.ALIGN_PARENT_END)
                marginEnd = dp2px(2f).toInt()
            }
        }
    }

    companion object {
        fun newInstance() = LanguageSelectFragment()
    }
}


class LanguageSelectActivity(): BaNoActivity<SingleFrameLayoutBinding>() {

    override fun initPage(savedInstanceState: Bundle?) {
        super.initPage(savedInstanceState)
        addFragment(LanguageSelectFragment.newInstance(), R.id.container)
    }

    override fun isSetStatusbar(): Boolean {
        return true
    }

    override fun isDarkMode(): Boolean {
        return true
    }

    companion object {
        fun jump(context: Context, isRecreate: Boolean = true) {
            context.startActivity(Intent(context, LanguageSelectActivity::class.java).apply {
                putExtra(IS_RECREATE, isRecreate)
            })
        }
    }

}